package com.doubao.device.exception;

import com.doubao.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 设备模块异常处理器，继承公共异常处理器并添加设备特有的异常处理
 */
@RestControllerAdvice
@Slf4j
public class DeviceExceptionHandler extends com.doubao.common.exception.GlobalExceptionHandler {

    /**
     * 处理资源未找到异常
     */
    @ExceptionHandler(ResourceNotFoundException.class)
    public Result<Void> handleResourceNotFoundException(ResourceNotFoundException e) {
        log.warn("资源未找到: {}", e.getMessage());
        return Result.failed(e.getMessage());
    }

    /**
     * 处理权限不足异常
     */
    @ExceptionHandler(PermissionDeniedException.class)
    public Result<Void> handlePermissionDeniedException(PermissionDeniedException e) {
        log.warn("权限不足: {}", e.getMessage());
        return Result.failed(e.getMessage());
    }

    /**
     * 处理数据重复异常
     */
    @ExceptionHandler(DuplicateDataException.class)
    public Result<Void> handleDuplicateDataException(DuplicateDataException e) {
        log.warn("数据重复: {}", e.getMessage());
        return Result.failed(e.getMessage());
    }

    /**
     * 处理设备模块业务异常
     * 注意：由于该异常与common模块中的BusinessException不同，需要单独处理
     */
    @ExceptionHandler(BusinessException.class)
    public Result<Void> handleDeviceBusinessException(BusinessException e) {
        log.warn("设备业务异常: {}", e.getMessage());
        return Result.failed(e.getMessage());
    }
}